package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.config.AliPay;
import cn.itsource.ymcc.constant.OrderStatusConstants;
import cn.itsource.ymcc.constant.PayConstants;
import cn.itsource.ymcc.domain.PayFlow;
import cn.itsource.ymcc.domain.PayOrder;
import cn.itsource.ymcc.mapper.PayFlowMapper;
import cn.itsource.ymcc.mapper.PayOrderMapper;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.service.IPayOrderService;
import cn.itsource.ymcc.vo.PayVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Case;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zongchangjiang
 * @since 2023-07-08
 */
@Service
@Slf4j
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements IPayOrderService {
    @Autowired
    private AliPay aliPay;
    @Autowired
    private PayFlowMapper payFlowMapper;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @Override
    public JsonResult apply(PayVo payVo) {
        Integer payType = payVo.getPayType();

        PayOrder payOrder = baseMapper.selectOne(new LambdaQueryWrapper<PayOrder>()
                .eq(PayOrder::getOrderNo, payVo.getOrderNo()));

        String url = null;
        switch (payType){
            case PayConstants.PAY_TYPE_BALANCE:
                url = null;break;
            case PayConstants.PAY_TYPE_ALI_PAY:
                String pay = aliPay.pay(payOrder.getSubject(),payOrder.getOrderNo(),payOrder.getAmount().toString(),payVo.getCallUrl());
                url = pay;break;
            case PayConstants.PAY_TYPE_WECHAT:
                url = null;break;
            default:
                url = null;break;
        }
        return JsonResult.success().setData(url);
    }

    @Override
    public void resultProcessing(Map<String, String> parameter) throws Exception {
        String out_trade_no = parameter.get("out_trade_no");
        PayOrder payOrder = baseMapper.selectOne(new LambdaQueryWrapper<PayOrder>().eq(PayOrder::getOrderNo, out_trade_no));
        if(payOrder == null){
            log.error("支付宝回调=该"+out_trade_no+"订单号 数据查询失败");
            //MQ的消息  发送短信给管理员
            //记录数据库
        }

        //修改支付单
        payOrder.setUpdateTime(new Date());
        payOrder.setPayStatus(OrderStatusConstants.ORDER_OK);
        baseMapper.updateById(payOrder);

        //添加流水
        PayFlow payFlow = new PayFlow();
//        交易时间
        String parameter1 = parameter.get("gmt_create");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = simpleDateFormat.parse(parameter1);
        payFlow.setNotifyTime(parse);
//        标题
        payFlow.setSubject(payOrder.getSubject());
//        交易号，对应订单号
        payFlow.setOutTradeNo(payOrder.getOrderNo());
//        金额
        payFlow.setTotalAmount(payOrder.getAmount());
//        交易状态
        payFlow.setTradeStatus(parameter.get("trade_status"));
//        支付状态
        payFlow.setPaySuccess(OrderStatusConstants.ORDER_OK);
//        结果描述
        payFlow.setResultDesc("支付成功");
        payFlowMapper.insert(payFlow);

        //发送一个消息 给课程和订单进行消费
        //topic  tag
        // message  ==>  我们要传什么东西
        rocketMQTemplate.syncSend(PayConstants.TOPIC_PAY_ORDER+":"+PayConstants.TAGS_PAY_ORDER,
                MessageBuilder.withPayload(payOrder.getOrderNo()).build());

    }
}
